package com.team.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.student.entity.*;
import com.team.student.mapper.GradeMapper;
import com.team.student.mapper.StuMapper;
import com.team.student.service.FindStuAndGrade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.beans.Transient;
import java.util.List;
@Service
public class FindStuAndGradeIml implements FindStuAndGrade {
    @Autowired
    private StuMapper stuMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Override
    public List<Stu> FindStuAndGrade() {
        return this.stuMapper.getAllStuAndGrade();
    }

    @Override
    public Integer delStu(Integer studentno) {
        return this.stuMapper.deleteByPrimaryKey(studentno);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public List<Grade> FindAllGrade() {
        return this.gradeMapper.selectByExample(new GradeExample());
    }

    @Transactional
    @Override
    public void delStuAndGrade(Integer gradeid) {
        stuMapper.delStuByGradeid(gradeid);

       /* int m = 3,n=0;
        int s=m/n;*/

        gradeMapper.deleteByPrimaryKey(gradeid);

    }

    @Override
    public List<Grade> getAllGrade() {
        return gradeMapper.selectByExample(new GradeExample());

    }

    @Override
    public void addStu(Stu stu) {
        stuMapper.insertSelective(stu);

    }

    @Override
    public Stu findSingleStu(Integer studentno) {
        return stuMapper.selectByPrimaryKey(studentno);
    }

    @Override
    public void updateStu(Stu stu) {
        stuMapper.updateByPrimaryKeySelective(stu);
    }

    /**
     *
     * @param pageNo 页码
     * @param pageSize 每页的大小
     * @return PageInfo<Stu>
     */
    @Override
    public PageInfo<Stu> getAllStuLim(Integer pageNo, Integer pageSize) {
        //开启分页  每页三条
        PageHelper.startPage(pageNo,pageSize);

        //List<Stu> list = this.stuMapper.selectByExample(new StuExample());
        //查询所有信息
        List<Stu> list = this.stuMapper.getAllStuAndGrade();
        //获取分页相关信息
        PageInfo<Stu> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public PageInfo<Stu> findStuCondition(StuCondition stuCondition) {

        PageHelper.startPage(stuCondition.getPageNo(),stuCondition.getPageSize());
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        /**
         * StringUtils字符串工具类
         */
        if (!StringUtils.isEmpty(stuCondition.getName()))
            criteria.andStudentnameLike("%"+stuCondition.getName()+"%");

        if (!StringUtils.isEmpty(stuCondition.getStartAge()))
            criteria.andAgeGreaterThanOrEqualTo(stuCondition.getStartAge());

        if (!StringUtils.isEmpty(stuCondition.getEndAge()))
            criteria.andAgeLessThanOrEqualTo(stuCondition.getEndAge());

        List<Stu> list = this.stuMapper.selectByExample(stuExample);
        PageInfo<Stu> stuPageInfo = new PageInfo<>(list);
        return stuPageInfo;


    }

    @Override
    public PageInfo<Stu> selectStuAndGrade(StuCondition stuCondition) {
        PageHelper.startPage(stuCondition.getPageNo(),stuCondition.getPageSize());


        List<Stu> list = stuMapper.selectStuAndGrade(stuCondition);
        PageInfo<Stu> stuPageInfos = new PageInfo<>(list);
        return stuPageInfos;
    }


}
